﻿using System.Collections.Generic;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using NHibernate;
using NHibernate.Criterion;

namespace Cms.Data
{
    public class SystemRepository : BaseRepository, ISystemRepository
    {
        public SystemRepository()
        {
        }
        
        public SystemRepository(ISession session)
            : base(session)
        {
        }

        #region ISystemRepository Members

        public IList<Plugin> GetPlugins()
        {
            return Session.CreateCriteria(typeof(Plugin)).List<Plugin>();
        }

        public IList<PluginTemplate> GetPluginTemplates(int siteID)
        {
            return Session.CreateQuery("from PluginTemplate pt where pt.Site.ID = " + siteID)
                .SetCacheable(true)
                .List<PluginTemplate>();
        }

        public void DeletePlugin(int id)
        {
            DeleteHql("from Plugin p where p.ID = " + id, "SystemRepository.DeletePlugin");
        }

        public void SavePlugin(Plugin plugin)
        {
            SaveEntity(plugin, "SystemRepository.SavePlugin");
        }

        public Plugin GetPlugin(int id)
        {
            return Session.Get<Plugin>(id);
        }
        
        public PluginTemplate GetPluginTemplate(int id)
        {
            return Session.Get<PluginTemplate>(id);
        }
        
        public IList<DashboardGadget> GetDashboardGadgets()
        {
            return Session.CreateCriteria(typeof(DashboardGadget)).List<DashboardGadget>();
        }
        
        public void DeleteDashboardGadget(int id)
        {
            DeleteHql("from DashboardGadget g where g.ID = " + id, "SystemRepository.DeleteDashboardGadget");
        }

        public DashboardGadget GetDashboardGadget(int id)
        {
            return Session.Get<DashboardGadget>(id);
        }

        public void SaveDashboardGadget(DashboardGadget gadget)
        {
            SaveEntity(gadget, "SystemRepository.SaveDashboardGadget");
        }

        public CustomAdminPage GetCustomAdminPage(int id)
        {
            return Session.Get<CustomAdminPage>(id);
        }

        public CustomAdminPage GetCustomAdminPageByFilename(string filename)
        {
            IList<CustomAdminPage> customAdminPages = Session.CreateCriteria(typeof (CustomAdminPage))
                    .Add(Restrictions.Eq("Filename", filename))
                    .List<CustomAdminPage>();
            return customAdminPages.Count == 1 ? customAdminPages[0] : null;
        }

        public void SaveCustomAdminPage(CustomAdminPage customAdminPage)
        {
            SaveEntity(customAdminPage, "SystemRepository.SaveCustomAdminPage");
        }

        public IList<CustomAdminPage> GetCustomAdminPages(int siteId)
        {
            return Session.CreateQuery("from CustomAdminPage where Site is null or Site.ID = :siteId order by Filename")
                    .SetInt32("siteId", siteId)
                    .List<CustomAdminPage>();
        }

        public void DeleteCustomAdminPage(int id)
        {
            DeleteHql("from CustomAdminPage where Id = " + id, "SystemRepository.DeleteCustomAdminPage");
        }

        public bool CustomAdminPageFilenameExists(int id, string filename)
        {
            string hql = "from CustomAdminPage where Filename = :filename";
            if (id > 0)
            {
                hql += " and Id != :id";
            }
            IQuery query = Session.CreateQuery(hql).SetString("filename", filename);
            if (id > 0)
            {
                query = query.SetInt32("id", id);   
            }           
            IList<CustomAdminPage> customAdminPages = query.List<CustomAdminPage>();
            return customAdminPages.Count > 0;
        }

        public void SaveFieldType(FieldType fieldType)
        {
            SaveEntity(fieldType, "SystemRepository.SaveFieldType");
        }

        #endregion
    }
}